package com.example.guard_toollib;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by tys on 2018/2/6.
 */

public class AES {


    //16字节
    static final String DEFAULT_PWD = "abcdefghijklmnop";
    //填充方式
    private static final String algorithmStr = "AES/ECB/PKCS5Padding";
    private static Cipher encryptCipher;
    private static Cipher decryptCipher;


    public static void main(String[] args) throws Exception {
        init(DEFAULT_PWD);

        File f = new File("C:\\Users\\tys\\Desktop\\test\\q.jpg");

        byte[] buf = encrypt(getBytes(f));
        File enFile = new File("C:\\Users\\tys\\Desktop\\test\\encrypt.jpg");
        FileOutputStream fo = new FileOutputStream(enFile);
        fo.write(buf);

        byte[] b = decrypt(getBytes(enFile));
        File deFile = new File("C:\\Users\\tys\\Desktop\\test\\decrypt.jpg");
        FileOutputStream df = new FileOutputStream(deFile);
        df.write(b);

        fo.close();
        df.close();
        String s = bytesToHex(encrypt("tys@123".getBytes()));
        System.out.println("加密:"+s);
    }

    public static void init(String password) {
        try {
            // 生成一个实现指定转换的 Cipher 对象。
            encryptCipher = Cipher.getInstance(algorithmStr);
            decryptCipher = Cipher.getInstance(algorithmStr);// algorithmStr
            byte[] keyStr = password.getBytes();
            SecretKeySpec key = new SecretKeySpec(keyStr, "AES");
            encryptCipher.init(Cipher.ENCRYPT_MODE, key);
            decryptCipher.init(Cipher.DECRYPT_MODE, key);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) {
            e.printStackTrace();
        }
    }

    public static byte[] encrypt(byte[] content) {
        try {
            byte[] result = encryptCipher.doFinal(content);
            return result;
        } catch (IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] decrypt(byte[] content) {
        try {
            byte[] result = decryptCipher.doFinal(content);
            return result;
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] getBytes(File file) throws Exception {
        try (RandomAccessFile r = new RandomAccessFile(file, "r")) {
            byte[] buffer = new byte[(int) r.length()];
            r.readFully(buffer);
            r.close();
            return buffer;
        }
    }

    private static String bytesToHex(byte[] bytes) {
        char[] digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        StringBuilder ret = new StringBuilder(bytes.length * 2);
        char[] cs = new char[2];
        for (byte b : bytes) {
            cs[0] = digit[b >> 4 & 0x0F];
            cs[1] = digit[b & 0x0F];
            ret.append(new String(cs));
        }
        return ret.toString();
    }


}
